//
//					StyleSimpleAgg.h
//

#ifndef __STYLE_SIMPLE_AGG_H
#define __STYLE_SIMPLE_AGG_H

#include "WidgetStyle.h"
#include "AggNewa.h"
#include "xagg_renderers.h"
#include "xagg_font.h"
#include "WindowImpl.h"

#include "Button.h"
#include "Label.h"
#include "Frame.h"

#include <agg_path_storage.h>
#include <agg_rounded_rect.h>
#include <agg_conv_stroke.h>

#include <sstream>

inline agg::rgba makeColor( unsigned char r, unsigned char g, unsigned char b, unsigned char a = 255 )
{
	return agg::rgba( r / 255.0, g / 255.0, b / 255.0, a / 255.0 );
}

namespace newa
{
	namespace WidgetStyles
	{
		class SimpleAgg : public WidgetStyle
		{
		public:
			SimpleAgg()
				: m_button_painted(0),
				m_frame_painted(0),
				m_label_painted(0)
			{}

			virtual void paintButton( const Button& btn, PixBuf& pb )
			{
				
				++m_button_painted;
				newa_agg::RenBase rb( pb );
				Rect r( btn.rect().size() );
				r.deflate( 1 );
				//DrawBevel( rb.rbase(), r );

				agg::rounded_rect rr( r.x, r.y, r.x + r.width, r.y + r.height, 7 );

				agg::conv_stroke< agg::rounded_rect > stroke( rr );
				stroke.width(2);

				xagg::SolidRenderer ren( rb.rbase() );
				ren.color( makeColor( 247, 236, 0, 50 ) );
				ren.render( rr );

				ren.color( agg::rgba( 0, 0, 0 ) );
				ren.render( stroke );
				
			}

			virtual void paintLabel( const Label& lb, PixBuf& pb )
			{
				++m_label_painted;
				static int TimesPainted = 0;
				++TimesPainted;
				newa_agg::RenBase rb( pb );
				Rect r( lb.rect().size() );
				agg::rounded_rect rr( r.x, r.y, r.x + r.width, r.y + r.height, 2 );
				agg::conv_stroke< agg::rounded_rect > stroke( rr );
				stroke.width(2);

				xagg::SolidRenderer ren( rb.rbase() );
				ren.color( makeColor( 247, 236, 0 ) );
				//ren.color( agg::rgba( 1.0, 1.0, 1.0 ) );
				//ren.render( rr );

				ren.color( agg::rgba( 0.1, 0.07, 0.04 ) );
				//ren.render( stroke );

				std::basic_stringstream< CharType > sstr;
				
				sstr << lb.text() << _T("L{") << m_label_painted << _T("}")
					<< _T("B{") << m_button_painted << _T("}")
					<< _T("F{") << m_frame_painted << _T("}");
				
				xagg::Font font( "Verdana", 16 );
				int w = font.textWidth( sstr.str() );
				int h = font.fontHeight();
				ren.color( agg::rgba( 0.0, 0.0, 0.0 ) );
				font.renderText( ren, ( lb.rect().width - w ) / 2, ( lb.rect().height + h ) / 2,
					sstr.str());
			}

			virtual void paintFrame( const Frame& frm, PixBuf& pb )
			{
				++m_frame_painted;
				newa_agg::RenBase rb( pb );
				Rect r( frm.rect().size() );
				r.deflate( 2 );
				agg::rounded_rect rr( r.x, r.y, r.x + r.width, r.y + r.height, 2 );

				agg::conv_stroke< agg::rounded_rect > stroke( rr );
				stroke.width(4);

	
				xagg::GradientRenderer< xagg::Gradient<> > gren( rb.rbase() );
				gren.grad().setColors( makeColor( 250, 242, 144 ), makeColor( 16, 14, 112 ), 16 );
				gren.grad().direct( r.x, r.y, r.x + r.width, r.y + r.height );
				gren.render( rr );

				xagg::SolidRenderer ren( rb.rbase() );
				//ren.color( makeColor( 250, 242, 144 ) );
				ren.color( agg::rgba( 0, 0, 0 ) );
				ren.render( stroke );
			}

		virtual void paintFrameHeader( const FrameWithHeader& frame, PixBuf& pb )
		{
			newa_agg::RenBase rb( pb );
			Rect r( frame.header()->rect().size() );
			agg::rounded_rect rr( r.x, r.y, r.x + r.width, r.y + r.height, 2 );

			xagg::GradientRenderer< xagg::Gradient<> > gren( rb.rbase() );
			gren.grad().setColors( makeColor( 255, 0, 0 ), makeColor( 0, 0, 255 ), 16 );
			gren.grad().direct( r.x, 0, r.x + r.width, 0 );
			gren.render( rr );
		}
		
		virtual void paintFrameClientArea( const FrameWithHeader& frame, PixBuf& pb )
		{
			newa_agg::RenBase rb( pb );
			Rect r( frame.clientArea()->rect().size() );
			agg::rounded_rect rr( r.x, r.y, r.x + r.width, r.y + r.height, 2 );

			xagg::GradientRenderer< xagg::Gradient<> > gren( rb.rbase() );
			gren.grad().setColors( makeColor( 0, 255, 0 ), makeColor( 255, 255, 0 ), 16 );
			gren.grad().direct( 0, r.y, 0, r.y + r.height );
			gren.render( rr );
		}

		private:
			int m_button_painted;
			int	m_frame_painted;
			int	m_label_painted;
		};
	}

	inline WidgetStyle& SimpleAggStyle()
	{
		return Loki::SingletonHolder< WidgetStyles::SimpleAgg >::Instance();
	}
}

/*
		void DrawRect( xagg::ren_base& rb, const Rect& r, agg::rgba color )
		{
			agg::path_storage path;
			path.move_to( r.x, r.y );
			path.hline_rel( r.width );
			path.vline_rel( r.height );
			path.hline_rel( -r.width );
			path.close_polygon();
			agg::conv_stroke< agg::path_storage > stroke( path );
			stroke.width( 0.5 );

			xagg::SolidRenderer ren( rb );
			ren.color( color );
			ren.render( stroke );
		}

		void PaintRect( xagg::ren_base& rb, const Rect& r, agg::rgba color )
		{
			agg::path_storage path;
			path.move_to( r.x, r.y );
			path.hline_rel( r.width );
			path.vline_rel( r.height );
			path.hline_rel( -r.width );
			path.close_polygon();

			xagg::SolidRenderer ren( rb );
			ren.color( color );
			ren.render( path );
		}

		void DrawPath( xagg::ren_base& rb, agg::path_storage& path, agg::rgba color )
		{
			agg::conv_stroke< agg::path_storage > stroke( path );
			stroke.width( 0.5 );

			xagg::SolidRenderer ren( rb );
			ren.color( color );
			ren.render( stroke );
		}

		void DrawBevel( xagg::ren_base& rb, const Rect& r   )
		{
			agg::rgba color( 0, 0, 0 );
			Rect r1( r );
			r1.deflate( 2 );
			DrawRect( rb, r1, color );
			r1.deflate( 1 );
			double cval = 224.0 / 256.0;
			color = agg::rgba( cval, cval, cval );
			PaintRect( rb, r1, color );
			cval *= 0.5;
			agg::rgba darken_color( cval, cval, cval ); //DarkenColor( color );
			r1.deflate( 1 );
			agg::path_storage path;
			path.move_to( r1.x, r1.y + r1.height );
			path.hline_rel( r1.width );
			path.vline_rel( - r1.height );
			DrawPath( rb, path, darken_color );
			r1 = r;
			r1.deflate( 1 );
			//PaintRect( rb, r1, darken_color );
			path.move_to( r1.x, r1.y + r1.height - 1 );
			path.hline_rel( r1.width - 1 );
			path.vline_rel( 1 - r1.height );
			DrawPath( rb, path, darken_color );
		}
*/

#endif
